<template>
<div :id="id">
  <el-card shadow="never"
           :style="{height: gadgetHeight + 'px'}"
           class="aui-card--fill">
    <div>
      <div class="cardHeader" @mouseenter="mouseenter(true)" @mouseleave="mouseenter(false)"
           v-if="(gadgetSetting.headerTitle && gadgetSetting.headerTitle.trim().length>0) || (gadgetSetting.headerDescription && gadgetSetting.headerDescription.trim().length>0)">
              <span>
                <span
                  class="cardTitle threepoints" :title="gadgetSetting.headerTitle"
                  v-if='gadgetSetting.headerTitle && gadgetSetting.headerTitle.trim().length>0'>{{ gadgetSetting.headerTitle }}</span>
                <el-tooltip :open-delay="1000" class="item"
                            effect="dark"
                            :content="gadgetSetting.headerDescription"
                            placement="right"
                            v-if='gadgetSetting.headerDescription && gadgetSetting.headerDescription.trim().length>0'>
                <i class="cuifont icon_help_circle cardIcon"></i>
              </el-tooltip>
              </span>
              <MoreMenu :slicer='slicer' :gadgetId='id' :gadgetType='gadgetInfo.com' @updateQuery="query" @fullScreen="fullScreen"/>
      </div>
    </div>
    <div align='center'
         v-loading="loading">
      <ejs-circulargauge :title="gadgetSetting.title" :titleStyle="{color:gadgetSetting.titleColor}" v-if="isHasData">
        <e-axes>
          <e-axis :radius='gadgetSetting.gaugeRadius'
                  :startAngle='gadgetSetting.startAngle'
                  :endAngle='gadgetSetting.endAngle'
                  :majorTicks='gadgetSetting.majorTicks'
                  :lineStyle='gadgetSetting.lineStyle'
                  :minorTicks='gadgetSetting.minorTicks'
                  :labelStyle='gadgetSetting.labelStyle'
                  :ranges='gadgetSetting.ranges'>
            <e-pointers>
              <e-pointer :value='value'
                         :radius='gadgetSetting.pointerRadius'
                         :pointerWidth='gadgetSetting.pointerWidth'
                         :cap='gadgetSetting.cap'
                         :needleTail='gadgetSetting.needleTail'>
              </e-pointer>
            </e-pointers>
          </e-axis>
        </e-axes>
      </ejs-circulargauge>
      <div v-else
           class="gadget-no-data">
        <el-image @click="query()" :src='noDataImageUrl'
                  fit='fill'></el-image>
      </div>
      <model :showSetting="queryModelDefaultSetting.showSetting" @queryModelClosed="queryModelClosed"
             :dataInfo="queryModelDefaultSetting.dataInfo" :updateGadget="upgradeGadgetSetting" :slicer='slicer'/>
    </div>
  </el-card>
</div>
</template>

<script>
import mixinGadgetViewModule from '@/mixins/gadget-view-module'

import { defaultGadgetSettingList } from '@/utils/data-json'

export default ({
  mixins: [mixinGadgetViewModule],
  components: { model: () => import('@/components/gadget/query-model/query-model') },
  data: function () {
    return {
      theme: 'Material',
      value: 0,
      gadgetSetting: null,
      loading: false,
      queryModelDefaultSetting: {
        showSetting: false,
        dataInfo: {
          businessId: this.pageId,
          gadgetId: this.id,
          gadgetType: 'circulargauge',
          gadgetSetting: this._.find(defaultGadgetSettingList, { gadgetType: 'circulargauge' }).gadgetSetting
          // gadgetSetting: {
          //   gaugeRadius: '80%',
          //   startAngle: 230,
          //   endAngle: 130,
          //   majorTicks: {
          //     width: 0
          //   },
          //   lineStyle: { width: 10, color: 'transparent' },
          //   minorTicks: {
          //     width: 0
          //   },
          //   ranges: [
          //     {
          //       start: 0,
          //       end: 30,
          //       color: '#30B32D'
          //     },
          //     {
          //       start: 30,
          //       end: 70,
          //       color: '#FFDD00'
          //     },
          //     {
          //       start: 70,
          //       end: 100,
          //       color: '#F03E3E'
          //     }
          //   ],
          //   labelStyle: {
          //     font: {
          //       fontFamily: 'Roboto',
          //       size: '12px',
          //       fontWeight: 'Regular'
          //     },
          //     offset: -5
          //   },
          //   pointerRadius: '60%',
          //   pointerWidth: 7,
          //   cap: {
          //     radius: 8,
          //     border: { width: 0 }
          //   },
          //   needleTail: {
          //     length: '25%'
          //   },
          //   headerTitle: '',
          //   headerDescription: ''
          // }
        }
      }
    }
  },

  created () {
    this.gadgetHeight = (this.gadgetInfo.h * 20) - 20
    this.query()
    this.subscribeMessage('runReport', this.query)
    this.$store.state.isWidget = false
  },
  methods: {
    queryModelClosed () {
      this.queryModelDefaultSetting.showSetting = false
      this.query()
    },
    showQueryModel () {
      this.queryModelDefaultSetting.showSetting = true
    },
    resized (i, newH, newW, newHPx, newWPx) {
      this.gadgetHeight = (newH * 20) - 20
      this.gadgetWidth = newWPx
    },
    query () {
      this.gadgetSetting = this.queryModelDefaultSetting.dataInfo.gadgetSetting
      this.loading = true
      var _self = this
      var param = {
        ...this.slicer,
        code: this.code,
        gadgettype: this.wgadgettype,
        widgetName: this.wName,
        pageId: this.pageId,
        gadgetId: this.id,
        filter: this.getFilterStr(),
        type: this.filterType
      }
      this.$interface.reports.getReportDataInfo(param).then(({ data: res }) => {
        _self.verifyHasData(res)
        if (res.config) {
          _self._.merge(_self.gadgetSetting, res.config)
        }
        _self.value = res.data[0][_self.gadgetSetting.valueFieldsName]

        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    }
  }
})
</script>
<style scoped>
.el-card ::v-deep .el-card__body {
  padding: 0px !important;
}

.el-card ::v-deep .el-divider--horizontal {
  margin: 3px 0 !important;
}

.threepoints {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cardHeader {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 20px;
  border-bottom: 1px solid #eee;
  position: relative;
}

.cardHeader > span {
  display: inline-block;
  width: calc(100% - 30px);
}

.cardTitle {
  font-size: 16px;
  color: #000;
  font-weight: 700;
  display: inline-block;
  max-width: calc(100% - 30px);
  position: relative;
  top: 2px;
}

.cardIcon {
  font-size: 16px;
  font-weight: 600;
  margin-left: 10px;
  position: relative;
  top: -2px;
}
</style>
